package com.example.smartall.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smartall.R;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.entity.HouseDetailsBean;
import com.example.smartall.ui.base.BaseActivity;
import com.example.smartall.utils.Constant;
import com.google.gson.Gson;

/**
  * @Author:         abbas
  * @CreateDate:     2022/2/7
  * @Description:    详情页面分别展示房源图片、房源名称、建筑面积、房源单价、房源类型、房源介绍等信息。
  *                  底部展示主页和打电话按钮，点击“主页”，返回找房主主页，点击“打电话”可以直接系统拨号页面，联系对应详情中的电话号码。
  *
  *                  直接拨号，好家伙
 *                   22点搞完收工
  */
public class HouseDetailsActivity extends BaseActivity implements View.OnClickListener {

    private int houseId = -1;

    private ImageView imBack;
    private TextView tvTitle;
    private ImageView img;
    private TextView tvAddress;
    private TextView tvContent;
    private Button btBack;
    private Button btCall;

    private HouseDetailsBean.DataBean dataBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_details);
        //套个娃
        houseId = getIntent().getIntExtra("houseId",houseId);

        init();
        initEvent();
        getHouseDetails();
    }

    @Override
    public void init() {
        imBack = (ImageView) findViewById(R.id.im_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        img = (ImageView) findViewById(R.id.img);
        tvAddress = (TextView) findViewById(R.id.tv_address);
        tvContent = (TextView) findViewById(R.id.tv_content);
        btBack = (Button) findViewById(R.id.bt_back);
        btCall = (Button) findViewById(R.id.bt_call);

    }

    @Override
    public void initEvent() {
        imBack.setOnClickListener(this);
        btBack.setOnClickListener(this);
        btCall.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.im_back:
            case R.id.bt_back:
                HouseDetailsActivity.this.finish();
                break;
            case R.id.bt_call:
                //申请拨打电话权限
                if (ContextCompat.checkSelfPermission(HouseDetailsActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
                    ActivityCompat.requestPermissions(HouseDetailsActivity.this,new String[]{Manifest.permission.CALL_PHONE},1);
                }else {
                    call();
                }
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    call();
                }else {
                    showToast("拒绝权限将无法使用程序");
                }
                break;
        }
    }

    private void call() {
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:"+dataBean.getTel()));
        startActivity(intent);
    }

    private void getHouseDetails(){
        Api.config(ApiConfig.HOUSE_DETAILS,null).getRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                HouseDetailsBean bean = new Gson().fromJson(res,HouseDetailsBean.class);
                if (bean != null && bean.getCode() == 200){
                    dataBean = bean.getData();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Glide.with(HouseDetailsActivity.this)
                                    .load(ApiConfig.BASE_URL + dataBean.getPic())
                                    .into(img);
                            tvAddress.setText(dataBean.getAddress());
                            tvContent.setText("建筑面积："+dataBean.getAreaSize()+
                                    "\n房源单价："+dataBean.getPrice()+
                                    "\n房源类型："+dataBean.getHouseType()+
                                    "\n房源结束："+dataBean.getDescription());
                        }
                    });
                }else {
                    Log.i(Constant.TAG,res);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        },""+houseId);
    }
}